import { createRouter, createWebHashHistory } from 'vue-router'
/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  // {
  //   path: '/redirect',
  //   component: Layout,
  //   hidden: true,
  //   children: [
  //     {
  //       path: '/redirect/:path(.*)',
  //       component: () => import('@/views/redirect/index.vue')
  //     }
  //   ]
  // },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  // vue-router 4.x  通配符配置是如下配置不在是 *
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        component: () => import('@/views/dashboard'),
        name: 'dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

// import auth from './modules/auth'
// import community from './modules/community'
// import content from './modules/content'
// import device from './modules/device'
// import expenses from './modules/expenses'
// import ticket from './modules/ticket'
// import house from './modules/house'
// import owners from './modules/owners'
// import parking from './modules/parking'
// import service from './modules/service'
// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  // auth,
  // community,
  // content,
  // device,
  // expenses,
  // ticket,
  // house,
  // owners,
  // parking,
  // service,
  {
    path: "/auth",
    component: Layout,
    meta: {
      title: "权限管理",
      icon: "dashboard",
    },
    name: "auth",
    redirect: "/auth/menu",
    children: [
      {
        path: "menu",
        component: () => import('@/views/auth/menu'),
        name: "authMenu",
        meta: {
          title: "菜单管理",
          icon: "dashboard"
        }
      },
      {
        path: "role",
        component: () => import('@/views/auth/role'),
        name: "authRole",
        meta: {
          title: "角色管理",
          icon: "dashboard"
        }
      },
      {
        path: "user",
        component: () => import('@/views/auth/user'),
        name: "authUser",
        meta: {
          title: "用户管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: '',
    component: Layout,
    redirect: '/community',
    children: [
      {
        path: '/community',
        component: () => import('@/views/community'),
        name: 'community',
        meta: { title: '小区管理', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: "/house",
    component: Layout,
    meta: {
      title: "房产管理",
      icon: "dashboard",
    },
    name: "house",
    redirect: "/house",
    children: [
      {
        path: "building",
        component: () => import('@/views/house/building'),
        name: "houseBuilding",
        meta: {
          title: "楼栋管理",
          icon: "dashboard"
        }
      },
      {
        path: "index",
        component: () => import('@/views/house/index'),
        name: "houseIndex",
        meta: {
          title: "房产管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: "/owners",
    component: Layout,
    meta: {
      title: "业主管理",
      icon: "dashboard",
    },
    name: "owners",
    redirect: "/owners",
    children: [
      {
        path: "member",
        component: () => import('@/views/owners/member'),
        name: "ownersMember",
        meta: {
          title: "人员管理",
          icon: "dashboard"
        }
      },
      {
        path: "vehicle",
        component: () => import('@/views/owners/vehicle'),
        name: "ownersVehicle",
        meta: {
          title: "车辆管理",
          icon: "dashboard"
        }
      },
      {
        path: "pet",
        component: () => import('@/views/owners/pet'),
        name: "ownersPet",
        meta: {
          title: "宠物管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: "/parking",
    component: Layout,
    meta: {
      title: "停车位管理",
      icon: "dashboard",
    },
    name: "parking",
    redirect: "/parking",
    children: [
      {
        path: "index",
        component: () => import('@/views/parking/index'),
        name: "parkingIndex",
        meta: {
          title: "车位管理",
          icon: "dashboard"
        }
      },
      {
        path: "usage",
        component: () => import('@/views/parking/usage'),
        name: "parkingUsage",
        meta: {
          title: "车位使用管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: "/service",
    component: Layout,
    meta: {
      title: "服务管理",
      icon: "dashboard",
    },
    name: "service",
    redirect: "/service",
    children: [
      {
        path: "activity",
        component: () => import('@/views/service/activity'),
        name: "serviceActivity",
        meta: {
          title: "活动管理",
          icon: "dashboard"
        }
      },
      {
        path: "repair",
        component: () => import('@/views/service/repair'),
        name: "serviceRepair",
        meta: {
          title: "报修管理",
          icon: "dashboard"
        }
      },
      {
        path: "complain",
        component: () => import('@/views/service/complain'),
        name: "serviceComplain",
        meta: {
          title: "投诉管理",
          icon: "dashboard"
        }
      },
      {
        path: "mailbox",
        component: () => import('@/views/service/mailbox'),
        name: "serviceMailbox",
        meta: {
          title: "信箱管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: "/expenses",
    component: Layout,
    meta: {
      title: "收费管理",
      icon: "dashboard",
    },
    name: "expenses",
    redirect: "/expenses",
    children: [
      {
        path: "project",
        component: () => import('@/views/expenses/project'),
        name: "expensesProject",
        meta: {
          title: "收费项目管理",
          icon: "dashboard"
        }
      },
      {
        path: "index",
        component: () => import('@/views/expenses/index'),
        name: "expensesIndex",
        meta: {
          title: "收费项目管理",
          icon: "dashboard"
        }
      },

    ],
  },
  {
    path: "/device",
    component: Layout,
    meta: {
      title: "资产设备管理",
      icon: "dashboard",
    },
    name: "device",
    redirect: "/device",
    children: [
      {
        path: "index",
        component: () => import('@/views/device/index'),
        name: "deviceIndex",
        meta: {
          title: "设备管理",
          icon: "dashboard"
        }
      },
      {
        path: "maintain",
        component: () => import('@/views/device/maintain'),
        name: "deviceMaintain",
        meta: {
          title: "维修管理",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: "/content",
    component: Layout,
    meta: {
      title: "内容管理",
      icon: "dashboard",
    },
    name: "content",
    redirect: "/content",
    children: [
      {
        path: "adv",
        component: () => import('@/views/content/adv'),
        name: "contentAdv",
        meta: {
          title: "广告管理",
          icon: "dashboard"
        }
      },
      {
        path: "cate",
        component: () => import('@/views/content/cate'),
        name: "contentCate",
        meta: {
          title: "资讯分类",
          icon: "dashboard"
        }
      },
      {
        path: "article",
        component: () => import('@/views/content/article'),
        name: "contentArticle",
        meta: {
          title: "资讯管理",
          icon: "dashboard"
        }
      },
      {
        path: "notice",
        component: () => import('@/views/content/notice'),
        name: "contentNotice",
        meta: {
          title: "通知公告",
          icon: "dashboard"
        }
      },
    ],
  },
  {
    path: '',
    component: Layout,
    redirect: '/ticket',
    children: [
      {
        path: '/ticket',
        component: () => import('@/views/ticket'),
        name: 'ticket',
        meta: { title: '投票管理', icon: 'dashboard', affix: true }
      }
    ]
  },

]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes: [...constantRoutes, ...dynamicRoutes],  // 只挂载了静态路由
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
});

export default router;
